package com.xiaouyudeguang.common.resource;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.xiaouyudeguang.common.constants.Headers;
import com.xiaouyudeguang.common.respose.ResultDto;
import com.xiaouyudeguang.common.utils.CodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/sms")
public class SmsResource {

    @PostMapping(value = "/{channel}/{phone}")
    public ResultDto<Object> login(@RequestHeader(Headers.TENANT_ID) String tenantId,
                                   @RequestHeader(Headers.APP_ID) String appId,
                                   @PathVariable("channel") String channel,
                                   @PathVariable("phone") String phone) throws Exception {
        try {
            SendSmsResponse response = CodeUtils.send(tenantId, appId, channel, phone);
            return "OK".equals(response.getCode()) ? ResultDto.ok(response.getBizId()) : ResultDto.failure(response.getMessage());
        } catch (Exception e) {
            return ResultDto.failure(e.getMessage());
        }
    }
}

